﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Celeriq.Common;

namespace Celeriq.Profiler
{
	public partial class MainForm : Form
	{
		private bool _isConnected = false;
		private Timer _timer = new Timer();
		private UserCredentials _credentials = null;
		private string _publicKey = string.Empty;
		private string _serverName = string.Empty;
		private long _lastProfileId = 0;
		private int _lineNumber = 0;
		private int _totalRows = 0;
		private bool _isRunning = false;

		public MainForm()
		{
			InitializeComponent();

			_timer.Stop();

			lvwItem.Columns.Add("Action");
			lvwItem.Columns.Add("Repository Id");
			lvwItem.Columns.Add("Repository Name");
			lvwItem.Columns.Add("Start time");
			lvwItem.Columns.Add("Duration");
		}

		private void timer_Tick(object sender, EventArgs e)
		{

		}

		public UserCredentials Credentials
		{
			get { return _credentials; }
			set { _credentials = value; }
		}

		public string PublicKey
		{
			get { return _publicKey; }
			set { _publicKey = value; }
		}

		public bool IsRunning
		{
			get { return _isRunning; }
			set
			{
				_isRunning = value;
				if (_isRunning)
					_timer.Start();
				else
					_timer.Stop();
			}
		}

		public void StartProfiling()
		{
			_timer = new Timer();
			_timer.Tick += new EventHandler(timer_Tick);
			_timer.Interval = 1000;
			_timer.Start();
		}

		public string ServerName
		{
			get { return _serverName; }
			set
			{
				_serverName = value;
				toolConnection.Text = _serverName;
			}
		}

		private void UpdateStatus(string text, int lineNumber, int totalRows)
		{
			if (lineNumber == 0)
				toolLine.Text = string.Empty;
			else
				toolLine.Text = "Line: " + lineNumber.ToString("###,###,##0");
			
			if (totalRows == 0)
				toolRows.Text = string.Empty;
			else
				toolRows.Text = "Rows: " + totalRows.ToString("###,###,##0");

			toolStatus.Text = text;
		}

	}
}
